<template>
  <div>
    <TopFilter
      ref="topFilter"
      :list="topFilterList"
      :cols="3"
      labelWidth="90"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
    ></TopFilter>

    <FilterTable
        ref="table"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        :height="'auto'"
        :isSelectColumn="false"
        :isToperInfo="false"
        columnsRef="ysjStore2019100801Table"
        :onColumnsChange="columns => (this.columns = columns)"
      >
        <template slot="controls">
          <el-button size="small" type="primary"  @click="addStore">新增</el-button>
        </template>
      </FilterTable>

    <BaseDialog
      :visible.sync="visible"
      :title="(type == 'add' ? '仓库信息新增' : '仓库信息编辑')"
      destroyOnClose
      :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }"
    >
      <storeDetail :type="type" :rowData="row" @changeHandle="changeHandle" @close="closeHandler" />
    </BaseDialog>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { getStoreList } from '@/api/retail/basedata/store';
import storeDetail from './storeDetail';

export default {
  name: 'store',
  mixins: [dictionary],
  components: {storeDetail},
  data(){
    this.Table = null;
    return {
      columns: this.createTableColumns(),
      topFilterList: this.createTopFilters(),
      fetchApi: getStoreList,
      fetchParams: {},
      type: '',
      row: {},
      visible: false,
    }
  },
  methods: {
    createTopFilters(){
      return [
        {
          type: 'INPUT',
          label: '仓库代码',
          fieldName: 'storageCode',
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: '仓库名称',
          fieldName: 'storageName',
          placeholder: '请输入...'
        },
        {
          type: 'SELECT',
          label: '数据来源',
          fieldName: 'dataSources',
          placeholder: '请选择...',
          itemList: this.createDictList('1045')
        },
        {
          type: 'SELECT',
          label: '是否有效',
          fieldName: 'isValid',
          placeholder: '请选择...',
          itemList: this.createDictList('1001')
        }
      ]
    },
    createTableColumns(){
      return [
        {
          title: '序号',
          dataIndex: 'index',
          align: 'center',
          width: 70,
          fixed: 'left',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 115,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.editStore(props.row)}>
                  编辑
                </el-button>
              </div>
            );
          }
        },
        {
          title: '仓库代码',
          dataIndex: 'storageCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '仓库名称',
          dataIndex: 'storageName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '数据来源',
          dataIndex: 'dataSources',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1045'),
          render: props => {
            return <span>{this.createDictText(props.row.dataSources, '1045')}</span>;
          }
        },
        {
          title: '是否有效',
          dataIndex: 'isValid',
          sorter: true,
          filter: true,
          filterItems: this.createDictList('1001'),
          render: props => {
            return <span>{this.createDictText(props.row.isValid, '1001')}</span>;
          }
        }
      ]
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    closeHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    addStore(){
      this.type = 'add';
      this.visible = true;
    },
    editStore(row){
      this.row = row;
      this.type = 'edit';
      this.visible = true;
    }
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  }
}
</script>
